home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / cp1.zip / RSORT.C < prev    next >
C/C++ Source or Header  |  1993-06-10  |  3KB  |  89 lines

  1. ===========================================================================
  2.  BBS: The Abacus * HST/DS * Potterville, MI
  3. Date: 06-06-93 (09:31)             Number: 122
  4. From: BRENDA HOLLOWAY              Refer#: 85
  5.   To: JEFFERY FOY                   Recvd: NO  
  6. Subj: Reverse sorting?               Conf: (36) C Language
  7. ---------------------------------------------------------------------------
  8.  JF> Say you have a text file like this:
  9.  
  10.  JF> first
  11.  JF> second
  12.  JF> third
  13.  JF> ...
  14.  JF> six-hundred-ninety-eight
  15.  JF> six-hundred-ninety-nine
  16.  JF> seven thousand
  17.  
  18.  
  19.  JF> How would you reverse the lines (i.e. in the example, seven thousand
  20.  JF> would become the first line, six-hundred-ninety-nine would become the
  21.  JF> second line ... first would become the last line).
  22.  
  23. If you have Infinite Stack Space (or a small file), you could do it recursively,
  24.  since to reverse any collection of lines, you save off the first line, reverse
  25. all the others, then write this line out at the end. It works, but is Not Recomm
  26. ended! 8) However, it's the easiest, and was one of the programs we had to write
  27.  in my LISP class.
  28.  
  29. If it's possible to read the entire file into memory, it becomes trivial. Read t
  30. he file in and write it out in reverse order. We'll assume you can't do THAT eit
  31. her!
  32.  
  33. A slower way to do it is to create a temporary file the same size as your input,
  34.  then use fseek to write the lines to it, from the end, backwards. Here's a quic
  35. k program that does this - it reads the input from stdin, so it uses a second te
  36. mporary file to buffer the input.
  37.  
  38. // Reverse the lines from stdin, and write them to stdout.
  39. // usage: REVERSE <input-file >output-file
  40. // by Brenda Holloway
  41.  
  42. #include <stdlib.h>
  43. #include <stdio.h>
  44.  
  45. void main()
  46. {
  47.         char temp1[] = "TEMP1.$$$";
  48.         char temp2[] = "TEMP2.$$$";
  49.         FILE *tchan, *tchan2;
  50.  
  51.         tchan = fopen( temp1, "wb+" );
  52.         if( !tchan ) perror( temp1 );
  53.         else
  54.         {
  55.                 tchan2 = fopen( temp2, "wb+" );
  56.                 if( !tchan2 ) perror( temp2 );
  57.         else
  58.                 {
  59.                         char iline[256];
  60.                         long count=0;
  61.  
  62.                         while( fgets( iline, (sizeof iline), stdin ) )
  63.                         {
  64.                                 count += strlen(iline);
  65.                                 fputs( iline, tchan );
  66.                                 fputs( iline, tchan2 );
  67.                         }
  68.                         fseek( tchan, 0, SEEK_SET );
  69.                         while( fgets( iline, (sizeof iline), tchan ) )
  70.                         {
  71.                                 fseek( tchan2, (count-=strlen(iline)), SEEK_SE
  72.                                 fputs( iline, tchan2 );
  73.                         }
  74.                         fseek( tchan2, 0, SEEK_SET );
  75.                         while( fgets( iline, (sizeof iline), tchan2 ) )
  76.                                 fputs( iline, stdout );
  77.                         fclose( tchan ); fclose( tchan2 );
  78.                         unlink( temp1 ); unlink( temp2 );
  79.                 }
  80.         }
  81. }
  82.  
  83.  
  84. --- Maximus 2.01wb
  85.  * Origin: Two Birds -- One Stone BBS (1:216/37)
  86. SEEN-BY: 1/211 11/2 4 13/13 101/1 108/89 109/25 110/69 114/5 123/19 124/1
  87. SEEN-BY: 153/752 154/40 77 157/110 159/100 125 430 575 950 203/23 209/209
  88. SEEN-BY: 261/1023 280/1 390/1 396/1 5 15 2270/1 2440/5 3603/20
  89.